from flask import Flask, render_template, request
import pandas as pd
import cufflinks as cf
import plotly as py
import plotly.graph_objs as go


app = Flask(__name__)



df = pd.read_csv ("hurun_unicorn.tsv", encoding = "utf8", sep="\t")

cf.set_config_file(offline=True, theme="ggplot")
py.offline.init_notebook_mode()

df_中国 = pd.read_csv ("hurun.csv", encoding = "utf8", sep="\t")




@app.route('/',methods=['GET'])
def hu_run_select() -> 'html':
    df_summary = df.groupby("行业").agg({"企业名称":"count","估值（亿人民币）":"sum","成立年份":"mean"}).sort_values(by = "企业名称",ascending = False )

    # 交互式可视化画图
    fig = df.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True)
    py.offline.plot(fig, filename="templates/example.html",auto_open=False)
    with open("templates/example.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())


    # data_str = dfs.to_html()
    return render_template('样式.html',
                            the_plot_all = plot_all,
                           )




@app.route('/chengshi',methods=['GET'])
def hu_run_select1() -> 'html':

    # 交互式可视化画图
    fig = df.iplot(kind="bar", x="城市", y="估值（亿人民币）", asFigure=True)
    py.offline.plot(fig, filename="templates/example1.html",auto_open=False)
    with open("templates/example1.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())


    # data_str = dfs.to_html()
    return render_template('样式.html',
                            the_plot_all = plot_all,
                           )



@app.route('/Cchengshi',methods=['GET'])
def hu_run_select2() -> 'html':

    # 交互式可视化画图
    fig = df_中国.iplot(kind="bar", x="城市", y="估值（亿人民币）", asFigure=True)
    py.offline.plot(fig, filename="templates/example2.html",auto_open=False)
    with open("templates/example2.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())

    # data_str = dfs.to_html()
    return render_template('样式.html',
                            the_plot_all = plot_all,
                           )


@app.route('/region',methods=['GET'])
def hu_run_select3() -> 'html':

    # 交互式可视化画图
    fig = df_中国.iplot(kind="bar", x="region", y="估值（亿人民币）", asFigure=True)
    py.offline.plot(fig, filename="templates/example3.html",auto_open=False)
    with open("templates/example3.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())


    # data_str = dfs.to_html()
    return render_template('样式.html',
                            the_plot_all = plot_all,
                           )



if __name__ == '__main__':
    app.run(debug=True)
